//
//  EFDistributorViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/15.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFDistributorViewController.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "EFConfirmView.h"
#import "iToast.h"

@interface EFDistributorViewController ()

@property(strong,nonatomic)UILabel* nameLabel;
@property(strong,nonatomic) UITextField *nameTextF;
@property(strong,nonatomic)UIView* nameLineView;

@property(strong,nonatomic)UILabel* phoneLabel;
@property(strong,nonatomic) UITextField* phoneTextF;
@property(strong,nonatomic)UIView* phoneLineView;

@property(strong,nonatomic)UILabel* staffLabel;
@property(strong,nonatomic) UIButton *staffYesBtn;
@property(strong,nonatomic) UIButton *staffNoBtn;
@property(strong,nonatomic)UIView* staffLineView;


@property(strong,nonatomic)UILabel* reasonLabel;
@property(strong,nonatomic)UITextField* reasonTextF;
@property(strong,nonatomic)UIView* reasonLineView;



@property(strong,nonatomic) UIButton *agreeBtn;
@property(strong,nonatomic)UILabel* introductionLabel;

@property(strong,nonatomic) UIButton *addBtn;

@property(strong,nonatomic)EFConfirmView* confirmView;


@property (strong, nonatomic) TPKeyboardAvoidingScrollView * scrollView;
 
@end

@implementation EFDistributorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"申请成为分销商";
    }
    return self;
}



-(void)initView{
    self.scrollView.backgroundColor = [UIColor whiteColor]; 
    
    [self.scrollView addSubview:self.nameLabel];
    [self.scrollView addSubview:self.nameTextF];
    [self.scrollView addSubview:self.nameLineView];
    
    [self.scrollView addSubview:self.phoneLabel];
    [self.scrollView addSubview:self.phoneTextF];
    [self.scrollView addSubview:self.phoneLineView];
    
    [self.scrollView addSubview:self.staffLabel];
    [self.scrollView addSubview:self.staffYesBtn];
    [self.scrollView addSubview:self.staffNoBtn];
    [self.scrollView addSubview:self.staffLineView];
    
    [self.scrollView addSubview:self.reasonLabel];
    [self.scrollView addSubview:self.reasonTextF];
    [self.scrollView addSubview:self.reasonLineView];
    
    [self.scrollView addSubview:self.agreeBtn];
    [self.scrollView addSubview:self.introductionLabel];
    
    [self.scrollView addSubview:self.addBtn];
    [self.view addSubview:self.scrollView];
}

-(void)showToastMessage:(NSString*) message{
    [[iToast makeText:message] show];
}

-(void)onClickAdd{
    NSString* name =  self.nameTextF.text;
    if(name == nil || name.length <=0){
        [self showToastMessage:@"联系人不能为空"];
        return;
    }
    
    NSString* phone =  self.phoneTextF.text;
    if(phone == nil || phone.length <=0){
        [self showToastMessage:@"电话不能为空"];
        return;
    }
    
    if(!self.staffYesBtn.selected  && ! self.staffNoBtn.selected){
        [self showToastMessage:@"是否为公司员工不能为空"];
        return;
    }
    NSString* is_employee =@"1";
    if(self.staffNoBtn.selected){
        is_employee =@"0";
    }
    
    NSString* reason =  self.reasonTextF.text;
    if(reason == nil || reason.length <=0){
        [self showToastMessage:@"申请原因不能为空"];
        return;
    }
    
    if(!self.agreeBtn.selected){
        [self showToastMessage:@"未同意说明"];
        return;
    }
    
    [APP_CONTEXT.net applyDistributorWithName:name phone:phone reason:reason employee:is_employee OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            [APP_CONTEXT.accountManager persistenceAccountInfo];

            [[NSNotificationCenter defaultCenter] postNotificationName:IC_REQUEST_DISTRIBUTOR_NOTIFICATION object:nil];
            
            [[UIApplication sharedApplication].keyWindow addSubview:self.confirmView];
        }else{
            [[iToast makeText:message]show];
        }
    }];
}

-(void)onClickStaffBtn:(id) sender{
    UIButton* btn = (UIButton*)sender;
    btn.selected = !btn.selected;
}

-(TPKeyboardAvoidingScrollView*)scrollView{
    if(_scrollView == NULL){
        _scrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame: CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _scrollView.contentSize =  CGSizeMake(kScreenWidth, kScreenHeight - kStatusBarHeight);
    }
    return _scrollView;
}


-(UILabel *)nameLabel{
    if(_nameLabel == NULL){
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _nameLabel.left = 20;
        _nameLabel.top = 19;
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor = kBlack333;
        
        NSMutableAttributedString * strIntro = [[NSMutableAttributedString alloc]initWithString:@"姓名*"];
        NSRange range = NSMakeRange(2,1);
        [strIntro addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [_nameLabel setAttributedText:strIntro];
        [_nameLabel sizeToFit];
    }
    return _nameLabel;
}

-(UITextField *)nameTextF{
    if(_nameTextF == NULL){
        _nameTextF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 120, 17)];
        _nameTextF.left = self.nameLabel.right + 50;;
        _nameTextF.top = 19;
        _nameTextF.font = [UIFont systemFontOfSize:15];
        _nameTextF.textColor = kBlack999;
        _nameTextF.keyboardType = UIKeyboardTypeDefault;
        _nameTextF.placeholder = @"请填写联系人";
    }

    return _nameTextF;
}

-(UIView *)nameLineView{
    if(_nameLineView == NULL){
        _nameLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _nameLineView.left = 20;
        _nameLineView.top = self.nameLabel.bottom + 17;
        _nameLineView.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _nameLineView;
}


-(UILabel *)phoneLabel{
    if(_phoneLabel == NULL){
        _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _phoneLabel.left = 20;
        _phoneLabel.top = self.nameLineView.bottom + 19;
        _phoneLabel.font = [UIFont systemFontOfSize:15];
        _phoneLabel.textColor = kBlack333;
        
        NSMutableAttributedString * strIntro = [[NSMutableAttributedString alloc]initWithString:@"手机号*"];
        NSRange range = NSMakeRange(3,1);
        [strIntro addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [_phoneLabel setAttributedText:strIntro];
        [_phoneLabel sizeToFit];
    }
    return _phoneLabel;
}

-(UITextField *)phoneTextF{
    if(_phoneTextF == NULL){
        _phoneTextF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 120, 17)];
        _phoneTextF.left = self.phoneLabel.right + 30;
        _phoneTextF.top = self.nameLineView.bottom + 19;
        _phoneTextF.font = [UIFont systemFontOfSize:15];
        _phoneTextF.textColor = kBlack999;
        _phoneTextF.keyboardType = UIKeyboardTypeDefault;
        _phoneTextF.placeholder = @"联系电话";
    }
    
    return _phoneTextF;
}

-(UIView *)phoneLineView{
    if(_phoneLineView == NULL){
        _phoneLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _phoneLineView.left = 20;
        _phoneLineView.top = self.phoneLabel.bottom + 17;
        _phoneLineView.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _phoneLineView;
}


-(UILabel *)staffLabel{
    if(_staffLabel == NULL){
        _staffLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _staffLabel.left = 20;
        _staffLabel.top = self.phoneLineView.bottom + 19;
        _staffLabel.font = [UIFont systemFontOfSize:15];
        _staffLabel.textColor = kBlack333;
        
        NSMutableAttributedString * strIntro = [[NSMutableAttributedString alloc]initWithString:@"公司员工*"];
        NSRange range = NSMakeRange(4,1);
        [strIntro addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [_staffLabel setAttributedText:strIntro];
        [_staffLabel sizeToFit];
    }
    return _staffLabel;
}

-(UIButton *)staffYesBtn{
    if(_staffYesBtn == NULL){
        _staffYesBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
        _staffYesBtn.left = self.staffLabel.right + 19;
        _staffYesBtn.top = self.phoneLineView.bottom + 19;
        [_staffYesBtn setImage:IMG(@"cartlist_Unselected_icon") forState:UIControlStateNormal];
        [_staffYesBtn setImage:IMG(@"cartlist_selected_icon") forState:UIControlStateSelected];
        [_staffYesBtn setTitle:@"是" forState:UIControlStateNormal];
        [_staffYesBtn setTitleColor:kBlack333 forState:UIControlStateNormal];
        _staffYesBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 30);
        _staffYesBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        
    
        [_staffYesBtn addTarget:self action:@selector(onClickStaffBtn:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _staffYesBtn;
}

-(UIButton *)staffNoBtn{
    if(_staffNoBtn == NULL){
        _staffNoBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
        _staffNoBtn.left = self.staffYesBtn.right + 35;
        _staffNoBtn.top = self.phoneLineView.bottom + 19;
        [_staffNoBtn setImage:IMG(@"cartlist_Unselected_icon") forState:UIControlStateNormal];
        [_staffNoBtn setImage:IMG(@"cartlist_selected_icon") forState:UIControlStateSelected];
        [_staffNoBtn setTitle:@"否" forState:UIControlStateNormal]; 
         [_staffNoBtn setTitleColor:kBlack333 forState:UIControlStateNormal];
        _staffNoBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 30);
        _staffNoBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        
        
        [_staffNoBtn addTarget:self action:@selector(onClickStaffBtn:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _staffNoBtn;
}

-(UIView *)staffLineView{
    if(_staffLineView == NULL){
        _staffLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _staffLineView.left = 20;
        _staffLineView.top = self.staffLabel.bottom + 17;
        _staffLineView.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _staffLineView;
}



-(UILabel *)reasonLabel{
    if(_reasonLabel == NULL){
        _reasonLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _reasonLabel.left = 20;
        _reasonLabel.top = self.staffLineView.bottom + 19;
        _reasonLabel.font = [UIFont systemFontOfSize:15];
        _reasonLabel.textColor = kBlack333;
        
        NSMutableAttributedString * strIntro = [[NSMutableAttributedString alloc]initWithString:@"申请原因*"];
        NSRange range = NSMakeRange(4,1);
        [strIntro addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [_reasonLabel setAttributedText:strIntro];
        [_reasonLabel sizeToFit];
    }
    return _reasonLabel;
}

-(UITextField *)reasonTextF{
    if(_reasonTextF == NULL){
        _reasonTextF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 120, 17)];
        _reasonTextF.left = self.reasonLabel.right + 20;;
        _reasonTextF.top = self.staffLineView.bottom + 19;
        _reasonTextF.font = [UIFont systemFontOfSize:15];
        _reasonTextF.textColor = kBlack999;
        _reasonTextF.keyboardType = UIKeyboardTypeDefault;
        _reasonTextF.placeholder = @"请填原因";
    }
    
    return _reasonTextF;
}

-(UIView *)reasonLineView{
    if(_reasonLineView == NULL){
        _reasonLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _reasonLineView.left = 20;
        _reasonLineView.top = self.reasonLabel.bottom + 17;
        _reasonLineView.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _reasonLineView;
}


-(UIButton *)agreeBtn{
    if(_agreeBtn == NULL){
        _agreeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        _agreeBtn.left = 18;
        _agreeBtn.top = self.reasonLineView.bottom + 19;
        [_agreeBtn setImage:IMG(@"cartlist_Unselected_icon") forState:UIControlStateNormal];
        [_agreeBtn setImage:IMG(@"cartlist_selected_icon") forState:UIControlStateSelected];
        
        
        [_agreeBtn addTarget:self action:@selector(onClickStaffBtn:) forControlEvents:UIControlEventTouchUpInside];
    }

    return _agreeBtn;
}

-(UILabel *)introductionLabel{
    if(_introductionLabel == NULL){
        _introductionLabel = [[UILabel alloc ]initWithFrame:CGRectMake(0, 0, 75, 17)];
        _introductionLabel.left = self.agreeBtn.right + 7;
        _introductionLabel.top = self.reasonLineView.bottom + 19;
        _introductionLabel.font = [UIFont systemFontOfSize:15];
        _introductionLabel.textColor = kBlack333;
        
        NSMutableAttributedString * strIntro = [[NSMutableAttributedString alloc]initWithString:@"已阅读说明"];
        NSRange range = NSMakeRange(3,2);
        [strIntro addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(0x69BA45) range:range];
        [_introductionLabel setAttributedText:strIntro];
        [_introductionLabel sizeToFit];
        
    }
    return _introductionLabel;
}


-(UIButton *)addBtn{
    if(_addBtn == NULL){
        _addBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        _addBtn.top = kScreenHeight - kStatusBarHeight -  50;
        _addBtn.backgroundColor = RGBCOLOR(0x69BA45);
        _addBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        _addBtn.titleLabel.textColor = [UIColor whiteColor];
        [_addBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(onClickAdd) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}


-(EFConfirmView *)confirmView{
    if(_confirmView == NULL){
        _confirmView = [[EFConfirmView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) Title:@"申请已提交" content:@"您已成功提交申请，请等待审核…审核通过“成为分销”会变为“我的分销”" block:^{
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
    return _confirmView;
}


@end
